home *** CD-ROM | disk | FTP | other *** search
/ SGI Freeware 1999 August / SGI Freeware 1999 August.iso / dist / fw_kdelibs.idb / usr / freeware / kde / include / ktoolboxmgr.h.z / ktoolboxmgr.h
Encoding:
C/C++ Source or Header  |  1999-01-26  |  9.7 KB  |  315 lines

  1. /*
  2.     This file is part of the KDE libraries
  3.     Copyright (C) 1998 Sven Radej (sven@lisa.exp.univie.ac.at)
  4.               
  5.     This library is free software; you can redistribute it and/or
  6.     modify it under the terms of the GNU Library General Public
  7.     License as published by the Free Software Foundation; either
  8.     version 2 of the License, or (at your option) any later version.
  9.  
  10.     This library is distributed in the hope that it will be useful,
  11.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  13.     Library General Public License for more details.
  14.  
  15.     You should have received a copy of the GNU Library General Public License
  16.     along with this library; see the file COPYING.LIB.  If not, write to
  17.     the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  18.     Boston, MA 02111-1307, USA.
  19. */
  20.  
  21. #ifndef K_OWN_WM
  22. #define K_OWN_WM
  23.  
  24. #include <qwindefs.h>
  25. #include <qwidget.h>
  26. #include <qtimer.h>
  27. #include <qlist.h>
  28. #include <qrect.h>
  29.  
  30. #include <X11/X.h>
  31. #include <X11/Xlib.h>
  32. #include <X11/Xutil.h>
  33.  
  34.  // $Id: ktoolboxmgr.h,v 1.3 1998/05/07 16:50:42 radej Exp $
  35.  // $Log: ktoolboxmgr.h,v $
  36.  // Revision 1.3  1998/05/07 16:50:42  radej
  37.  // Docs update: you don't get mouseRelease
  38.  //
  39.  // Revision 1.2  1998/05/04 16:39:34  radej
  40.  // Docs update.
  41.  //
  42.  // Revision 1.1  1998/04/28 09:16:41  radej
  43.  // Initial checkin
  44.  //
  45.  
  46.  /**
  47.   * KToolBoxManager is a class for self management of small windows.
  48.   * Windows can be resized or moved. During move/resize it will emit
  49.   * all changes and you can recompute and accept or discard new geometry.
  50.   * You can define hot spots on the screen, and you will receive signal
  51.   * when your widget enters it. KToolBoxManager doesn't draw any decoration.
  52.   *
  53.   * You will call (after constructing KToolBoxManager instance) doMove or
  54.   * doResize when mouse presses some handle point of your widget. Then,
  55.   * KToolBoxManager will move (or resize) hollow rectangle (or actual widget)
  56.   * on the screen. Moving/resizing is finished on mouse release event or
  57.   * by calling stop() function.
  58.   * Provided that your widget is top-level, the simplest use is like this:
  59.   * <pre>
  60.   * mousePressevent(QMouseEvent *)
  61.   * {
  62.   *  KToolBoxManager *mgr = new KToolBoxManager(this);
  63.   *  doMove();
  64.   * }
  65.   * </pre>
  66.   * Moving/resizing can be transparent or opaque. In transparent mode, word
  67.   * "resizer" refers to hollow rectangle which is actually resized or moved.
  68.   * In opaque mode it refers to actual widget.
  69.   *
  70.   * Moving child widgets in opaque mode will move the widget only inside your
  71.   * parent widget. You can drag it outside, but you won't see it, neither
  72.   * during drag nor after. You should reparent it in that case.
  73.   *
  74.   * Functions doMove and doResize do not return untill mouse is released or
  75.   * stop() function is called. However, this is QTimer driven so you will
  76.   * receive signals, and Qt-engine will operate normally. Halting does not
  77.   * hog CPU (it's not an empty for(;;) loop).
  78.   *
  79.   * You will NOT receive mouseRelease event when mouse is released.
  80.   *
  81.   * @short Class for own window management.
  82.   * @author Sven Radej <sven@lisa.exp.univie.ac.at>
  83.   *
  84.   */
  85. class KToolBoxManager : public QObject
  86. {
  87.   Q_OBJECT
  88.     
  89. public:
  90.   /**
  91.    * Constructor. widget is the widget to be managed. It can be any custom
  92.    * widget with QWidget as a base class.
  93.    * If transparent is true (default) moving and resizing is transparent.
  94.    * @ref #doMove or @ref #doResize won't return till mouseRelease, or
  95.    * @ref #stop . Qt will run normaly, because this thing is QTimer driven.
  96.    * You can get position and size calling @ref #x , @ref #y , @ref #width and
  97.    * @ref #height
  98.    */
  99.   KToolBoxManager (QWidget *widget, bool transparent=true);
  100.  
  101.   /**
  102.    * Destructor. If resizer is working, it will stop working,
  103.    * and move/resize the widget (i.e. will call @ref #stop) before it's death.
  104.    */
  105.   ~KToolBoxManager ();
  106.  
  107.   /**
  108.    * Starts moving. If dynamic is true signal @ref #posChanged
  109.    * will be emitted whenever position changes. If dynamic is false no signals
  110.    * are emitted except @ref #onHotSpot when resizer enters a hot spot area.
  111.    * Function will not return till end of drag. You
  112.    * can call @ref #setGeometry and resizer will adapt to it whenever you want.
  113.    * If dontmove is false, widget is moved to resizer's position when dragging
  114.    * If dontmove is false, no move is done you have to do it. Beware, moving
  115.    * of child widgets often isn't what you want. Still it is possible.
  116.    * If KToolBoxManager already moves or resizes widget when you
  117.    * call this function, it will return and do nothing.<br>
  118.    * When dynamic is true, signal @ref #posChanged is emitted when resizer
  119.    * changes position.<br>
  120.    * When in_hotspot_static is true, resizer is not moved while in hotspot;
  121.    * only mouse moves, and  @ref #posChanged is not emitted.
  122.    * Hint: Call this function with dynamic=false and define hot spots.
  123.    * @see #x
  124.    * @see #y
  125.    * @see #width
  126.    * @see #height
  127.    */
  128.   void doMove(bool in_hotspot_static=false, bool dynamic = false, bool dontmove=false);
  129.  
  130.   /**
  131.    * Starts resizing. If dynamic is true (default) signal @ref #sizeChanged
  132.    * will be emitted whenever size changes. If dynamic is false, no signals
  133.    * are emitted. Function will not return until
  134.    * button mouse is released or @ref #stop function is called. You can call
  135.    * @ref #setGeometry and resizer will adapt to it whenever you want.
  136.    * If dontresize is false, widget is resized to resizer's size on the end.
  137.    * If dontresize is true, widget is not resized, you have to do it. If
  138.    * KToolBoxManager already
  139.    * moves or resizes widget when you  call this function, it will return
  140.    * and do nothing.  When dynamic is true, signal @ref #sizeChanged is
  141.    * emitted only when resizers changes size. You can resize the resizer
  142.    * with @ref #setGeometry or @ref #resize .
  143.    * @see #x
  144.    * @see #y
  145.    * @see #width
  146.    * @see #height
  147.    */
  148.   void doResize(bool dynamic = false, bool dontresize=false);
  149.  
  150.   /**
  151.    * Adds region x, y, w, h to the lists of hot spots, and returns
  152.    * index of that hot spot. When resizer enters that hot spot, signal
  153.    * @ref #onHotSpot will be emitted. Only first hotspot is reported
  154.    * That mean if you have overlaping hotspots the one with the lower index
  155.    * will be reported. There is a special hotspot with index -1; exterior
  156.    * of all hotspots(the whole screen minus defined hotspots). When resizer
  157.    * leaves defined all hotspots @ref #onHotSpot (-1) will be emitted.
  158.    *
  159.    * Note that x and y must be global screen coordinates.
  160.    * @see #removeHotSpot
  161.    */
  162.   int addHotSpot(int x, int y, int w, int h);
  163.  
  164.   /**
  165.    * Removes hot spot index.
  166.    * @see #addHotSpot
  167.    */
  168.   void removeHotSpot(int index);
  169.  
  170.     /**
  171.    * Sets geometry of resizer. Does nothing if manager is not working (i.e.
  172.    * if @ref #doMove or @ref #doResize were not called first). This function
  173.    * will not emit signals @ref #sizeChanged ,  @ref #posChanged or
  174.    * @ref #onHotSpot . Call this after you received some of those signals,
  175.    * and you have to change geometry. For example, toolbar will change
  176.    * geometry if dragged on hot spots - to tell user that it will -  if left
  177.    * on that place -  re-embed itself into parent window. Note that x and y
  178.    * must be global screen coordinates.
  179.    */
  180.   void setGeometry(int x, int y, int w, int h);
  181.  
  182.   /**
  183.    * Sets geometry of resizer to geometry of hotspot index. This is
  184.    * provided for conevenience. Does nothing is index is false.
  185.    */
  186.   void setGeometry(int index);
  187.  
  188.   /**
  189.    * Resizes the resizer. rw is width and rh is height
  190.    */
  191.   void resize(int rw, int rh) {setGeometry(xp, yp, rw, rh);};
  192.  
  193.   /**
  194.    * Returns global x coordinate of a mouse.
  195.    */
  196.    int mouseX() {return rx;};
  197.  
  198.   /**
  199.    * Returns global y coordinate of a mouse.
  200.    */
  201.   int mouseY() {return ry;};
  202.   
  203.   /**
  204.    * Returns global x coordinate of resizer.
  205.    */
  206.   int x() {return xp;};
  207.  
  208.   /**
  209.    * Returns global y coordinate of resizer.
  210.    */
  211.   int y() {return yp;};
  212.  
  213.   /**
  214.    * Returns width of resizer.
  215.    */
  216.   int width() {return w;};
  217.  
  218.   /**
  219.    * Returns height resizer.
  220.    */
  221.   int height() {return h;};
  222.  
  223.   
  224. public slots:
  225.  
  226.   /**
  227.    * Calling this slot will stop the process of moving or resizing. It is
  228.    * equal as if user releases the mouse button.
  229.    */
  230.   void stop ();
  231.  
  232. protected:
  233.  
  234.   /**
  235.    * Internal - draws rectangle on the screen
  236.    */
  237.   void drawRectangle (int x, int y, int w, int h);
  238.  
  239.   /**
  240.    * Internal - deletes last rectangle, if there is one.
  241.    */
  242.   void deleteLastRectangle ();
  243.  
  244.   /**
  245.    * Internal - mode.
  246.    */
  247.   enum Mode {Nothing=0, Moving=1, Resizing=2};
  248.   
  249.  
  250. protected slots:
  251.   /**
  252.    * Internal, QTimer driven mover.
  253.    */
  254.   void doMoveInternal();
  255.  
  256.   /**
  257.    * Internal, QTimer driven sizer.
  258.    */
  259.   void doResizeInternal();
  260.   
  261. private:
  262.   int xp, yp, w, h;
  263.   int ox, oy, ow, oh;
  264.   int orig_x, orig_y, orig_w, orig_h;
  265.   bool noLast;
  266.   bool working;
  267.   bool dynamic;
  268.   bool geometryChanged;
  269.   bool transparent;;
  270.   bool dontmoveres;
  271.   bool deepSpace;
  272.   bool hotspot_static;
  273.   Mode mode;
  274.   
  275.   QWidget *widget;
  276.   QTimer *timer;
  277.  
  278.   QList<QRect> hotspots;
  279.   QRect *last_hsp;
  280.   
  281.   int rx, ry, sx, sy;
  282.   int offX, offY;
  283.  
  284.   /* X-stuff */
  285.  
  286.   Window root;
  287.   GC rootgc;
  288.   int scr;
  289.   XEvent ev;
  290.   unsigned int active_button;
  291.   
  292. signals:
  293.     
  294.     /**
  295.      * This signal is emitted when resizer changes position. Note:
  296.      * x and y are global screen coordinates
  297.      */
  298.     void posChanged (int x, int y);
  299.  
  300.     /**
  301.      * This signal is emitted when resizer changes size.
  302.      */
  303.     void sizeChanged (int w, int h);
  304.  
  305.     /**
  306.      * This signal is emitted when resizer enter hot spot index.
  307.      * It is also emited with index = -1 in the moment when resizer
  308.      * leaves a hot spot but doesn't enter another hotspot. That is,
  309.      * when it goes to "free space".
  310.      */
  311.     void onHotSpot (int id);
  312.  
  313. };
  314.  
  315. #endif